<?php
class Managermodel extends CI_Model{
	function getPosOption(){
		$query = $this->db->get('pos_option');
		return $query->result_array();
	}
	
	function getRealtimeUpload($pos,$date){
		$this->db->select('dateupload, larn, 
						posname as larnname, 
						sum(buynet1) as totalbuynet, 
						sum(net_wght) as totalnetwght,
						',FALSE);
		$this->db->from('truck');
		$this->db->join('pos_option','truck.larn = pos_option.posid');
		if ($pos){
			$this->db->where('larn',$pos);
		}
		if ($date){
			//$teststring = '2012-01-12 -> 2012-01-13';
			$startdate = substr($date, 0,10);
			$enddate =  substr($date, 14,10);
			if ($enddate == '') $enddate = $startdate;
			$dateRange = "date(dateupload) BETWEEN '$startdate' AND '$enddate'";
			$this->db->where($dateRange,NULL,FALSE);
		}else{
			//NOW() - INTERVAL 1 DAY
			$dateRange = 'dateupload BETWEEN NOW()-INTERVAL 1 DAY AND NOW()';
			$this->db->where($dateRange,NULL,FALSE);
		}
		$this->db->group_by('dateupload');
		$this->db->order_by('dateupload desc');
		$query = $this->db->get();
		//echo $this->db->last_query();
		return $query->result_array();
	}
	
function getRealtimeUploadDefault($limit=null){
		$this->db->select('dateupload, 
						posname, 
						sum_netwg, 
						sum_netbuy, 
						sum_promo,
						');
		$this->db->from('truck_sum_upload');
		$this->db->join('pos_option','truck_sum_upload.larn = pos_option.posid');
			//NOW() - INTERVAL 1 DAY
		$dateRange = 'dateupload BETWEEN NOW()-INTERVAL 1 DAY AND NOW()';
		$this->db->where($dateRange,NULL,FALSE);
		
		$this->db->order_by('dateupload desc');
		if($limit){
			$this->db->limit($limit);
		}
		
		$query = $this->db->get();
		//echo $this->db->last_query();
		return $query->result_array();
	}
	
function getSummary($year){
		$this->db->select('posname,
						MONTH(dateupload) as amonth, 
						SUM(sum_netwg) as month_netwg, 
						SUM(sum_netbuy) as month_netbuy, 
						',FALSE);
		$this->db->from('truck_sum_upload');
		$this->db->join('pos_option','truck_sum_upload.larn = pos_option.posid');
			//NOW() - INTERVAL 1 DAY
		$dateRange = 'YEAR(dateupload) = '.$year;
		$this->db->where($dateRange,NULL,FALSE);
		$this->db->group_by('amonth,posname');
		$this->db->order_by('posname asc,amonth asc');
		$query = $this->db->get();
		//echo $this->db->last_query();
		return $query->result_array();
	}
function getRangeUpload($pos,$date){
		$this->db->select('dateupload, 
						posname,
						sum_netwg, 
						sum_netbuy, 
						sum_promo,
						');
		$this->db->from('truck_sum_upload');
		$this->db->join('pos_option','truck_sum_upload.larn = pos_option.posid');
			//NOW() - INTERVAL 1 DAY
		if ($pos){
			$this->db->where('larn',$pos);
		}
		if ($date){
			//$teststring = '2012-01-12 -> 2012-01-13';
			$startdate = substr($date, 0,10);
			$enddate =  substr($date, 14,10);
			if ($enddate == '') $enddate = $startdate;
			$dateRange = "date(dateupload) BETWEEN '$startdate' AND '$enddate'";
			$this->db->where($dateRange,NULL,FALSE);
		}else{
			//NOW() - INTERVAL 1 DAY
			$dateRange = 'dateupload BETWEEN NOW()-INTERVAL 1 DAY AND NOW()';
			$this->db->where($dateRange,NULL,FALSE);
		}
		
		$this->db->order_by('dateupload desc');
		$query = $this->db->get();
		//echo $this->db->last_query();
		return $query->result_array();
	}
	
	function save_supply_payment($pos,$money,$paymenttype,$bankname){
		$data = array(
		   'pos' => $pos ,
		   'money' => $money ,
		   'paymenttype' => $paymenttype,
			'bankname' => $bankname
		);
		$this->db->insert('truck_larn_paid_log', $data);
		
		
		$sql = "UPDATE truck_larn_pay SET paidamount=paidamount+$money WHERE larn=$pos";
		//update larn paid
//		$this->db->select('paidamount');
//		$this->db->from('truck_larn_pay');
//		$this->db->where('larn',$pos);
//		$result = $this->db->get()->row_array();
//		$havepaid = $result['paidamount'];
//		
//		$this->db->set('paidamount',$havepaid+$money);
//		$this->db->where('larn',$pos);
//		$this->db->update('truck_larn_pay');
		$this->db->simple_query($sql);
		return true;
	}
	
//	function getSupplyBalance($pos){
//		$sql = "SELECT dateupload AS date, sum_netbuy_promo AS money, '-' as type 
//				FROM truck_sum_upload
//				WHERE larn = $pos 
//				UNION SELECT timestamp, money , '+'
//				FROM truck_larn_paid_log
//				WHERE pos = $pos 
//				ORDER BY date desc";
//		$supplybalance = $this->db->query($sql)->result_array();
//		return $supplybalance;
//	}

	function getSupplyBalance($larn){
		$this->db->from('truck_larn_transaction');
		$this->db->where('larn',$larn);
		$this->db->order_by('trans_date desc');
		return $this->db->get()->result_array();
	}
	
	function getPaymentDetail($paid_id){
		$query = $this->db->get_where('truck_larn_paid_log', array('paid_id' => $paid_id));
		return $query->row_array();
	}
}